//
//  HBaseLineView.m
//  SSIL
//
//  Created by 余胜民 on 16/5/11.
//  Copyright © 2016年 sanmy. All rights reserved.
//

#import "HBaseLineView.h"

@interface HBaseLineView ()
@property (nonatomic,strong) CAShapeLayer * shapeLayer;
@end

@implementation HBaseLineView

- (instancetype)init{
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor baseLineColor];
        _cornerRadius = 0;
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor baseLineColor];
        _cornerRadius = 0;
    }
    return self;
}

- (void)addLine{
    
    if (_lineType == BaseLineTypeSolid) {
        self.backgroundColor = [UIColor baseLineColor];
        
        if (_shapeLayer) {
            [_shapeLayer removeFromSuperlayer];
            _shapeLayer = nil;
        }
        
    }
    
    if (_lineType == BaseLineTypeDottedVertical) {
        
        self.backgroundColor = _spaceColor ? _spaceColor : [UIColor clearColor];
        
        if (!_shapeLayer) {
            _shapeLayer = [CAShapeLayer layer];
            [self.layer addSublayer:_shapeLayer];
        }
        UIBezierPath * path = [UIBezierPath bezierPath];
        [path moveToPoint:CGPointMake(_lineSize.width/2, 0)];
        [path addLineToPoint:CGPointMake(_lineSize.width/2, _lineSize.height)];
        
        _shapeLayer.path = path.CGPath;
        _shapeLayer.strokeColor = (_lineColor) ? (_lineColor.CGColor) : ([UIColor baseLineColor].CGColor);
        _shapeLayer.lineWidth = _lineSize.width;
        _shapeLayer.lineDashPattern = _spaceArr ? _spaceArr : @[@5,@2];
        _shapeLayer.lineJoin = kCALineJoinRound;
    }
    
    if (_lineType == BaseLineTypeDottedHorizontal) {
        
        self.backgroundColor = _spaceColor ? _spaceColor : [UIColor clearColor];
        
        
        if (!_shapeLayer) {
            _shapeLayer = [CAShapeLayer layer];
            [self.layer addSublayer:_shapeLayer];
        }
        UIBezierPath * path = [UIBezierPath bezierPath];
        [path moveToPoint:CGPointMake(0, _lineSize.height/2)];
        [path addLineToPoint:CGPointMake(_lineSize.width,_lineSize.height/2)];
        
        _shapeLayer.path = path.CGPath;
        _shapeLayer.strokeColor = (_lineColor) ? (_lineColor.CGColor) : ([UIColor baseLineColor].CGColor);
        _shapeLayer.lineWidth = _lineSize.height;
        _shapeLayer.lineDashPattern = _spaceArr ? _spaceArr : @[@5,@2];
        _shapeLayer.lineJoin = kCALineJoinRound;
    }
    
    if (_lineType == BaseLineTypeDottedAll) {
        
        self.backgroundColor = _spaceColor ? _spaceColor : [UIColor clearColor];
        
        
        if (!_shapeLayer) {
            _shapeLayer = [CAShapeLayer layer];
            [self.layer addSublayer:_shapeLayer];
        }
        
        CGMutablePathRef path = CGPathCreateMutable();
        CGPathMoveToPoint(path, NULL, 0, _lineSize.height - _cornerRadius);
        CGPathAddLineToPoint(path, NULL, 0, _cornerRadius);
        CGPathAddArc(path, NULL, _cornerRadius, _cornerRadius, _cornerRadius, M_PI, -M_PI_2, NO);
        CGPathAddLineToPoint(path, NULL, _lineSize.width - _cornerRadius, 0);
        CGPathAddArc(path, NULL, _lineSize.width - _cornerRadius, _cornerRadius, _cornerRadius, -M_PI_2, 0, NO);
        CGPathAddLineToPoint(path, NULL, _lineSize.width, _lineSize.height - _cornerRadius);
        CGPathAddArc(path, NULL, _lineSize.width - _cornerRadius, _lineSize.height - _cornerRadius, _cornerRadius, 0, M_PI_2, NO);
        CGPathAddLineToPoint(path, NULL, _cornerRadius, _lineSize.height);
        CGPathAddArc(path, NULL, _cornerRadius, _lineSize.height - _cornerRadius, _cornerRadius, M_PI_2, M_PI, NO);
        
        _shapeLayer.path = path;
        _shapeLayer.strokeColor = (_lineColor) ? (_lineColor.CGColor) : ([UIColor baseLineColor].CGColor);
        _shapeLayer.lineWidth = 0.5;
        _shapeLayer.fillColor = nil;
        _shapeLayer.lineDashPattern = _spaceArr ? _spaceArr : @[@5,@2];
        _shapeLayer.lineJoin = kCALineJoinRound;
        CGPathRelease(path);
    }
}

- (void)setLineType:(BaseLineType)lineType{
    _lineType = lineType;
    [self addLine];
}

- (void)setLineSize:(CGSize)lineSize{
    _lineSize = lineSize;
    [self addLine];
}

- (void)setLineColor:(UIColor *)lineColor{
    _lineColor = lineColor;
    [self addLine];
}

- (void)setSpaceArr:(NSArray *)spaceArr{
    _spaceArr = spaceArr;
    [self addLine];
}

- (void)setSpaceColor:(UIColor *)spaceColor{
    _spaceColor = spaceColor;
    [self addLine];
}

- (void)setCornerRadius:(CGFloat)cornerRadius{
    _cornerRadius = cornerRadius;
    [self addLine];
}


@end
